import requests


class WoniuSales:
    def __init__(self):
        self.s = requests.session()

    def homepage(self):
        # 打开首页
        self.s.get('http://localhost:8081/WoniuSales/')

    def login(self):
        # 登陆
        headers = {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}
        data = 'username=admin&password=admin123&verifycode=0000'
        r = self.s.post('http://localhost:8081/WoniuSales/user/login', data=data, headers=headers)
        content = r.text
        if content == 'login-pass':
            print('登陆成功.')
        else:
            print('登陆失败.')

    def add_customer(self, name, phone, sex, date, creditkids, creditcloth):
        # 添加会员
        headers = {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}
        data = 'customername=%s&customerphone=%s&childsex=%s&childdate=%s&creditkids=%d&creditcloth=%d' % (
        name, phone, sex, date, creditkids, creditcloth)
        r = self.s.post('http://localhost:8081/WoniuSales/customer/add', data=data.encode(), headers=headers)
        # 验证点可以有两个:一是响应内容, 二是状态码
        return r.text, r.status_code

    def add_customer_test(self, name, phone, sex, date, creditkids, creditcloth, exp_result, exp_status_code):
        self.login()
        actual_result, actual_status_code = self.add_customer(name, phone, sex, date, creditkids, creditcloth)
        print('########### 开始测试 ###########')
        if actual_result == exp_result:
            print('测试用例执行通过!')
        else:
            print('测试用例执行失败')
            print('状态码:' + '预期-' + str(exp_status_code) + '. ' '实际 -' + str(actual_status_code))
            print('响应:' + '预期 - ' + exp_result + '. ' + '实际 - ' + actual_result)
        print('########### 结束测试 ###########')


if __name__ == '__main__':
    ws = WoniuSales()
    ws.login()
    ws.add_customer()
